<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\exception\DbException;
use think\response\Json;

/**
 * 广电年度收入
 *
 * @icon fa fa-circle-o
 */
class Zjcaiwushow extends Backend
{

    /**
     * Zjcaiwushow模型对象
     * @var \app\admin\model\Zjcaiwushow
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Zjcaiwushow;
        $this->view->assign("ctypeList", $this->model->getCtypeList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        $input = input('param.');
        $filter = isset($input['filter'])?json_decode($input['filter'],true):'';
        if(empty($filter['year'])){
            $year = date('Y')-1;
            $year = 2024;
        }else{
            $year = $filter['year'];
        }

        $list = $this->model
            //->where($where)
            //->order($sort, $order)
            ->order('id asc')
            ->paginate(30);
        foreach($list as $row){
            if($row['shouru'] == '佣金'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = zjyj_find($v,$year,'yongjin');
                }
            }
            if($row['shouru'] == '网费') {
                $rang = range(1, 12);
                foreach ($rang as $v) {
                    $yue = 'yue' . $v;
                    $row->$yue = zjyj_find($v,$year,'wangfei');
                }
            }
                if($row['shouru'] == '移机'){
                    $rang = range(1,12);
                    foreach ($rang as $v){
                        $yue = 'yue'.$v;
                        $row->$yue = zjyj_find($v,$year,'yiji');
                    }
                }
                if($row['shouru'] == '其他' && $row['ctype'] == 1){
                    $rang = range(1,12);
                    foreach ($rang as $v){
                        $yue = 'yue'.$v;
                        $row->$yue = zjyj_find($v,$year,'ru_other');
                    }
                }

                if($row['shouru'] == '合计收入:'){
                    $rang = range(1,12);
                    foreach ($rang as $v) {
                        $yue = 'yue' . $v;
                        $arr = [$list[1][$yue], $list[2][$yue], $list[3][$yue]];
                        $sum = array_sum($arr);
                        $row->$yue = $sum==0?'':float_two($sum)+0;
                    }
                }
            if($row['shouru'] == '其他' && $row['ctype'] == 2){
                 $rang = range(1,12);
                 foreach ($rang as $v){
                     $yue = 'yue'.$v;
                     $row->$yue = zjyj_find($v,$year,'zhi_other');
                 }
             }
             if($row['shouru'] == '账号费'){
                 $rang = range(1,12);
                 foreach ($rang as $v){
                     $yue = 'yue'.$v;
                     $row->$yue = zjyj_find($v,$year,'zhanghao');
                 }
             }

             if($row['shouru'] == '档口员工工资'){
                 $rang = range(1,12);
                 foreach ($rang as $v){
                     $yue = 'yue'.$v;
                     $row->$yue = zjyj_find($v,$year,'gz');
                 }
             }
             if($row['shouru'] == '对公户其他收入：'){
                 $rang = range(1,12);
                 foreach ($rang as $v){
                     $yue = 'yue'.$v;
                     $row->$yue = zjyj_find($v,$year,'dg_shouru');
                 }
             }
             if($row['shouru'] == '合计支出：'){
                 $rang = range(1,12);
                 foreach ($rang as $v) {
                     $yue = 'yue' . $v;
                     $arr = [$list[6][$yue], $list[7][$yue], $list[8][$yue]];
                     $sum = array_sum($arr);
                     $row->$yue = $sum==0?'':float_two($sum)+0;
                 }
             }
            if($row['shouru'] == '利润：'){
                $rang = range(1,12);
                foreach ($rang as $v) {
                    $yue = 'yue' . $v;
                    if((float)$list[10][$yue]>0){
                        $n = ((float)$list[0][$yue]+(float)$list[4][$yue]+(float)$list[10][$yue])-(float)$list[9][$yue];
                        $row->$yue = $n?float_two($n):'0';
                    }else{
                        $row->$yue = '';
                    }
                }
            }
            $row->year = $year;
            $heji = array_sum([
                $row->yue1,$row->yue2,$row->yue3,$row->yue4,$row->yue5,$row->yue6,
                $row->yue7,$row->yue8,$row->yue9,$row->yue10,$row->yue11,$row->yue12]);
            $row->heji = $heji==0?'':float_two($heji);

        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }



}
